package com.common.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.entity.User;

public class MainInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		String path = request.getRequestURI();
		System.out.println("========================================="+request.getRequestURI());
		if(path.endsWith(".dat")){
			return true;
		}
		if(path.contains("ssjf")){
			if(path.contains("upload.html") || path.contains("user/me")){
				User user = (User) request.getSession().getAttribute("loggedUser");
				if(null != user) return true;
				else{
					response.sendRedirect("/SpringDemo/ssjf/page/login.html");
					return false;
				}
			}
			return true;
		}
		User user = (User) request.getSession().getAttribute("loggedIn");
		if(null != user) return true;
		System.out.println("拦截========================================="+request.getRequestURI());
		response.sendRedirect("/SpringDemo/bg/login");
		return false;
	}

}
